# Import PaddleOCR for offline deployment
from paddleocr import PaddleOCR

# Initialize the OCR model, supporting Chinese and English
ocr = PaddleOCR(use_angle_cls=True, lang='ch')  # This model supports both Chinese and English

def predict(imgPath, vehicle_info_database):
    predictResult = "未识别"
    plateNo = "未识别"
    carBrandZh = "未识别"
    original_image = cv2.imread(imgPath)
    
    v_color_model = load_model(v_color_model_path)
    v_type_model = load_model(v_type_model_path)
    
    # Load and predict the car's bounding box (use pre-trained YOLO or similar)
    # Your YOLO code for car detection goes here (you can also replace it with PaddleDetection)

    # Let's assume the detection box and process them as before
    # Extract the car's region of interest (ROI) where the plate might be located
    
    result_dict = parse_result(predict_result_dict) # Result post-processing

    for i in range(result_dict["b_box_num"]):
        x1_min = result_dict["detection_boxes"][i][0]
        y1_min = result_dict["detection_boxes"][i][1]
        x1_max = result_dict["detection_boxes"][i][2]
        y1_max = result_dict["detection_boxes"][i][3]
        carBrandZh = result_dict["detection_classes"][i] # Vehicle brand

        # Extract the lower 1/3 of the detected vehicle for plate detection
        left = x1_min
        top = int(y1_min + (y1_max - y1_min) * 0.67)
        right = x1_max
        bottom = y1_max
        crop_image = original_image[top:bottom, left:right]

        # Convert the cropped image to grayscale for model input
        grayImg = cv2.cvtColor(crop_image, cv2.COLOR_BGR2GRAY)
        roi_img = cv2.resize(grayImg, (VEHICLE_WIDTH, VEHICLE_HEIGHT))
        roi_img = roi_img.astype("float")/255.0
        roi_img = img_to_array(roi_img)
        roi_img = np.expand_dims(roi_img, axis=0)
        
        # Car type prediction
        (bus, car, minibus, truck) = v_type_model.predict(roi_img)[0]
        v_type_result = {"bus": bus, "car": car, "minibus": minibus, "truck": truck}
        v_type_label = max(v_type_result, key=v_type_result.get)

        # Car color prediction
        (black, blue, brown, green, red, silver, white, yellow) = v_color_model.predict(roi_img)[0]
        v_color_result = {"black": black, "blue": blue, "brown": brown, "green": green, "red": red, "silver": silver, "white": white, "yellow": yellow}
        v_color_label = max(v_color_result, key=v_color_result.get)

        # Use PaddleOCR to detect the plate number
        plate_result = ocr.ocr(crop_image)  # Perform OCR on the cropped image
        
        if plate_result:
            plateNo = plate_result[0][0][1][0]  # Extract the recognized text (plate number)
            inputCarInfo = [plateNo, carBrandZh]

            # Check if it's a fake plate by comparing it with the vehicle database
            isFake, true_car_brand = isFakePlate(inputCarInfo, vehicle_info_database)
            if isFake:
               predictResult = "这是一辆套牌车" 
            else:
               predictResult = "这是一辆正常车"    
        else:
            plateNo = "未识别"
            predictResult = "车牌未识别，无法判定" 
            
    return plateNo, v_type_label, v_color_label, carBrandZh, predictResult
